package com.example.springcode;


import com.example.springcode.beans.PropertyValue;
import com.example.springcode.beans.PropertyValues;
import com.example.springcode.beans.factory.BeanDefinition;
import com.example.springcode.beans.factory.BeanFactory;
import com.example.springcode.beans.factory.BeanReference;
import com.example.springcode.test.*;


public class SpringCodeApplication {

    public static void main(String[] args) {
        testBeanFactory();
        // testClassPath();
        // testContext();
        // testProxyBeanFactory();

    }

    private static void testBeanFactory() {
        BeanFactory beanFactory = new BeanFactory();

        // 注册UserDao
        beanFactory.registerBeanDefinition("userDao", new BeanDefinition(UserDao.class));

        // 注册代理的UserDao
        beanFactory.registerBeanDefinition("proxyUserDao", new BeanDefinition(ProxyBeanFactory.class));

        // 注册UserService
        PropertyValues propertyValues = new PropertyValues();
        propertyValues.addPropertyValue(new PropertyValue("id", "10001"));
        // 使用普通的UserDao
        propertyValues.addPropertyValue(new PropertyValue("userDao", new BeanReference("userDao")));
        // 使用代理的UserDao
        // propertyValues.addPropertyValue(new PropertyValue("userDao", new BeanReference("proxyUserDao")));
        beanFactory.registerBeanDefinition("userService", new BeanDefinition(UserService.class, propertyValues));

        // 注册BeanFactoryPostProcessor
        MyBeanFactoryPostProcessor beanFactoryPostProcessor = new MyBeanFactoryPostProcessor();
        beanFactoryPostProcessor.postProcessBeanFactory(beanFactory);

        // 注册BeanPostProcessor
        MyBeanPostProcessor beanPostProcessor = new MyBeanPostProcessor();
        beanFactory.addBeanPostProcessor(beanPostProcessor);

        // 获取UseService
        UserService userService = beanFactory.getBean("userService", UserService.class);
        userService.queryUserInfo();

        // System.out.println("BeanFactoryAware：" + userService.getBeanFactory());

        beanFactory.destroySingletons();
    }

    private static void testProxyBeanFactory() {
//        BeanFactory beanFactory = new BeanFactory();
//
//        beanFactory.registerBeanDefinition("proxyUserDao", new BeanDefinition(ProxyBeanFactory.class));
//        beanFactory.getBean("proxyUserDao");
//
//        PropertyValues propertyValues = new PropertyValues();
//        propertyValues.addPropertyValue(new PropertyValue("id", "10001"));
//        propertyValues.addPropertyValue(new PropertyValue("userDao", new BeanReference("proxyUserDao")));
//
//        beanFactory.registerBeanDefinition("userService", new BeanDefinition(UserService.class, propertyValues));
//
//        UserService userService = (UserService) beanFactory.getBean("userService");
        // userService.queryUserInfo();
    }
}
